<script setup>
import { ref, computed } from 'vue'
import { injectLanguage } from '../utils/language-store.js'

// 使用语言服务
const { currentLang, t } = injectLanguage()

// 新闻分类 - 使用computed确保语言切换时自动更新
const categories = computed(() => [
  { id: 'all', name: t('news.categories.all') },
  { id: 'company', name: t('news.categories.company') },
  { id: 'industry', name: t('news.categories.industry') },
  { id: 'tech', name: t('news.categories.tech') }
])

// 当前选中的分类
const selectedCategory = ref('all')

// 新闻数据
const newsList = [
  {
    id: 1,
    title: '公司荣获年度创新企业奖',
    excerpt: '在近日举办的行业盛典上，我们公司凭借卓越的技术创新能力和优质的服务荣获年度创新企业奖。',
    date: '2023-06-15',
    category: 'company',
    image: 'https://picsum.photos/600/400?random=1'
  },
  {
    id: 2,
    title: '人工智能技术在企业中的应用趋势',
    excerpt: '随着AI技术的不断发展，越来越多的企业开始探索人工智能在业务中的应用，本文将分析当前的主要趋势。',
    date: '2023-06-10',
    category: 'industry',
    image: 'https://picsum.photos/600/400?random=2'
  },
  {
    id: 3,
    title: 'Vue 3 Composition API 最佳实践',
    excerpt: 'Vue 3 Composition API 为我们提供了更灵活的组件开发方式，本文将分享一些在实际项目中的最佳实践。',
    date: '2023-06-05',
    category: 'tech',
    image: 'https://picsum.photos/600/400?random=3'
  },
  {
    id: 4,
    title: '公司新办公室正式启用',
    excerpt: '为了更好地服务客户和员工，我们公司全新的现代化办公空间正式启用，办公环境得到显著提升。',
    date: '2023-05-28',
    category: 'company',
    image: 'https://picsum.photos/600/400?random=4'
  },
  {
    id: 5,
    title: '云计算市场发展前景分析',
    excerpt: '云计算作为数字化转型的重要基础设施，其市场前景十分广阔，本文将对未来发展进行深入分析。',
    date: '2023-05-20',
    category: 'industry',
    image: 'https://picsum.photos/600/400?random=5'
  },
  {
    id: 6,
    title: '响应式设计在现代Web开发中的重要性',
    excerpt: '随着移动设备的普及，响应式设计已成为现代Web开发的必备技能，本文将探讨其关键技术和实现方法。',
    date: '2023-05-15',
    category: 'tech',
    image: 'https://picsum.photos/600/400?random=6'
  }
]

// 过滤后的新闻列表
const filteredNews = computed(() => {
  if (selectedCategory.value === 'all') {
    return newsList
  }
  return newsList.filter(news => news.category === selectedCategory.value)
})

// 格式化日期 - 使用computed确保语言切换时自动更新日期格式
const formatDate = computed(() => (dateString) => {
  const options = { year: 'numeric', month: 'long', day: 'numeric' }
  const locale = currentLang.value === 'en' ? 'en-US' : 'zh-CN'
  return new Date(dateString).toLocaleDateString(locale, options)
})
</script>

<template>
  <div class="news">
    <!-- 新闻资讯头部 -->
    <section class="news-header">
      <div class="container">
        <h1 v-text="t('news.pageTitle')"></h1>
        <p v-text="t('news.subtitle')"></p>
      </div>
    </section>

    <!-- 新闻分类 -->
    <section class="news-categories">
      <div class="container">
        <div class="category-tabs">
          <button 
            v-for="category in categories" 
            :key="category.id"
            :class="{ active: selectedCategory === category.id }"
            @click="selectedCategory = category.id"
          >
            <span v-text="category.name"></span>
          </button>
        </div>
      </div>
    </section>

    <!-- 新闻列表 -->
    <section class="news-list">
      <div class="container">
        <div class="news-cards">
          <div 
            class="news-card" 
            v-for="news in filteredNews" 
            :key="news.id"
          >
            <div class="news-image">
              <img :src="news.image" :alt="news.title">
            </div>
            <div class="news-content">
              <div class="news-meta">
                <span class="news-date" v-text="formatDate(news.date)"></span>
                <span class="news-category" :class="news.category" v-text="categories.find(c => c.id === news.category)?.name">
                </span>
              </div>
              <h3 v-text="news.title"></h3>
              <p v-text="news.excerpt"></p>
              <router-link :to="`/news/${news.id}`" class="read-more" v-text="t('news.readMore')">
              </router-link>
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<style scoped>
.news {
  font-family: 'Arial', sans-serif;
}

.news-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 100px 0;
  text-align: center;
  margin-bottom: 3rem;
  border-radius: 0 0 30px 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}

.news-header h1 {
  font-size: 3.5rem;
  margin-bottom: 1.5rem;
  font-weight: 800;
  letter-spacing: -0.5px;
}

.news-header p {
  font-size: 1.3rem;
  margin-bottom: 2.5rem;
  opacity: 0.95;
  max-width: 700px;
  margin-left: auto;
  margin-right: auto;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 新闻分类样式 */
.category-tabs {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 1rem;
  margin-bottom: 3rem;
}

.category-tabs button {
  padding: 12px 24px;
  background: white;
  border: 2px solid #e0e0e0;
  border-radius: 30px;
  cursor: pointer;
  font-size: 1.1rem;
  font-weight: 500;
  transition: all 0.3s ease;
  color: #555;
}

.category-tabs button:hover {
  border-color: #667eea;
  color: #667eea;
}

.category-tabs button.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: transparent;
  color: white;
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

/* 新闻卡片样式 */
.news-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  gap: 2.5rem;
  margin-bottom: 5rem;
}

.news-card {
  background: white;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.1);
  transition: all 0.4s ease;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.news-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
  border-color: rgba(102, 126, 234, 0.2);
}

.news-image {
  height: 220px;
  overflow: hidden;
}

.news-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.news-card:hover .news-image img {
  transform: scale(1.05);
}

.news-content {
  padding: 2rem;
}

.news-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.2rem;
  font-size: 0.9rem;
}

.news-date {
  color: #888;
}

.news-category {
  padding: 5px 12px;
  border-radius: 20px;
  font-weight: 500;
  background: #f0f0f0;
  color: #555;
}

.news-category.company {
  background: #e3f2fd;
  color: #1976d2;
}

.news-category.industry {
  background: #e8f5e9;
  color: #388e3c;
}

.news-category.tech {
  background: #f3e5f5;
  color: #7b1fa2;
}

.news-content h3 {
  font-size: 1.5rem;
  margin-bottom: 1rem;
  color: #333;
  font-weight: 700;
  line-height: 1.4;
}

.news-content p {
  color: #666;
  line-height: 1.7;
  margin-bottom: 1.5rem;
  font-size: 1.1rem;
}

.read-more {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
}

.read-more:hover {
  color: #764ba2;
  transform: translateX(5px);
}

@media (max-width: 768px) {
  .news-header {
    padding: 6rem 1.5rem;
  }
  
  .news-header h1 {
    font-size: 2.5rem;
  }
  
  .news-header p {
    font-size: 1.1rem;
  }
  
  .category-tabs {
    gap: 0.5rem;
  }
  
  .category-tabs button {
    padding: 10px 20px;
    font-size: 1rem;
  }
  
  .news-cards {
    grid-template-columns: 1fr;
    gap: 2rem;
  }
  
  .news-content {
    padding: 1.5rem;
  }
  
  .news-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }
}
</style>